import api from '../../http/modules/api.js';

export default {
    namespaced: true,
    state: {
        pages: 0,   //总页数
        total: 0,   //总数据条数
        studentsData: [],  //增加学生的信息
        searchData: {
            type: "name",
            value: "",
            pageSize: 4,
            currentPage: 1,
        },   //获取学生列表数据的限定条件
    },
    mutations: {
        //获取学生数据成功后，更新学生数据
        setGetStudentData(state, newData) {
            state.pages = newData.pages;
            state.total = newData.total;
            state.studentsData = newData.result;
        },
        // 翻页更改数据
        setCurrentPage(state, newData) {
            state.searchData.currentPage = newData;
        },
        // 更改每页条数
        setPageSize(state, newData){
            state.searchData.pageSize = newData;
        }
    },
    actions: {
        // 获取学生列表数据
        async getStudents(context) {
            const res = await api.students.getStudents(context.state.searchData);
            if (res.code) {
                // commit 触发 mutations 的方法
                context.commit('setGetStudentData', res.data)
            };
        },
        // 通过Id删除选中学生
        async deleteOneStudent(context, _id) {
            const res = await api.students.deleteOneStudent({
                _id
            });
            return res;
        },
        // 通过选中Id获取选中学生的信息
        async getOneStudentById(context, _id) {
            const res = await api.students.getOneStudentById({
                _id
            });
            return res
        },
        // 修改选中学生的信息
        async UpdateOneStudent(context, student) {
            const res = await api.students.UpdateOneStudent(student);
            return res;
        },
        // 增加学生
        async addOneStudent(context, addStudent) {
            const res = await api.students.addOneStudent(addStudent);
            return res;
        },
    },
}